package com.message.util;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName : ToExcel  //类名
 * @Description : 导出工具类  //描述
 * @Author : yangqi  //作者
 * @Date: 2021-03-01 12:43  //创建时间
 * @modifier: //修改人
 * @modificationTime: //修改时间
 */
public class ToExcel<T> {
    /**
     * 常规导出
     * @param filename 文件名
     * @param heards  头信息
     * @param response
     * @param list 数据集合
     */
    public void excel1(String filename, String[] heards, HttpServletResponse response, List<List<String>> list){
        Workbook workbook=new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("oneSheet");
        Row row = sheet.createRow(0);
        for (int i=0;i<heards.length;i++){
            row.createCell(i).setCellValue(heards[i]);
        }
        for(int i=0;i<list.size();i++){
            List<String> list1=new ArrayList<>();
            list1=list.get(i);
            Row row1=sheet.createRow(i+1);
            for (int j=0;j<list1.size();j++){
                row1.createCell(j).setCellValue(list1.get(j));
            }
        }
        try {
            response.setHeader("Content-disposition", "attachment;filename="+ URLEncoder.encode(filename,"UTF-8")+".xlsx");//附件形式下载
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        try {
            workbook.write(response.getOutputStream());  //写到客户端
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            workbook.close();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
    public void excel(String filename, String[] heards, HttpServletResponse response, List<List<String>> list)  {
        //声明一个workbook工作薄
        Workbook workbook=new XSSFWorkbook();
        //声明sheet工作表
        Sheet sheet=workbook.createSheet("one");
        //创建行
        Row row=sheet.createRow(0);
        for (int i = 0; i < heards.length; i++) {
            //创建头信息
//            Cell cell=null;
//            cell=row.createCell(i);
//            cell.setCellValue(heards[i]);
            row.createCell(i).setCellValue(heards[i]);
        }
        for (int i = 0; i < list.size(); i++) {
            List<String> stringList=list.get(i);
            Row row1=sheet.createRow(i+1);
            for (int i1 = 0; i1 < stringList.size(); i1++) {
                Cell cell=row1.createCell(i1);
                String value=stringList.get(i1);
                cell.setCellValue(value);
            }
        }
        try {
            response.setHeader("Content-disposition", "attachment;filename="+ URLEncoder.encode(filename,"UTF-8")+".xlsx");//附件形式下载
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        try {
            workbook.write(response.getOutputStream());  //写到客户端
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            workbook.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 反射实现导出
     * @param filename
     * @param heards
     * @param response
     * @param list
     * @param patt
     */
    public void excel(String filename, String[] heards, HttpServletResponse response, List<T> list,String patt)  {
        Workbook workbook=new XSSFWorkbook();
        Sheet sheet=workbook.createSheet("one");
        Row row=sheet.createRow(0);
        for (int i = 0; i < heards.length; i++) {
            row.createCell(i).setCellValue(heards[i]);
        }
        for (int i = 0; i < list.size(); i++) {
            T t=list.get(i);
            Field[] fields=t.getClass().getDeclaredFields();
            Row row1=sheet.createRow(i+1);
            int y=0;
            for (Field field : fields) {
                Cell cell=row1.createCell(y);
                String name="get"+field.getName().substring(0,1).toUpperCase()+field.getName().substring(1);
                try {
                    Method method=t.getClass().getMethod(name,new Class[]{});
                    Object value=method.invoke(t,new Class[]{});
                    if (value==null){
                        cell.setCellValue("");
                    } else if(value instanceof Date){
                        SimpleDateFormat simpleDateFormat=new SimpleDateFormat(patt);
                        cell.setCellValue(simpleDateFormat.format((Date) value));
                    }else {
                        cell.setCellValue(value.toString());
                    }
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
                y++;
            }
        }

        try {
            response.setHeader("Content-disposition", "attachment;filename="+ URLEncoder.encode(filename,"UTF-8")+".xlsx");//附件形式下载，文件名叫duty.xls
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        try {
            workbook.write(response.getOutputStream());  //写到客户端
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            workbook.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


}
